﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    DongHoConBLL bll = new DongHoConBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    DongHoConEntities DongHoConEntities = new DongHoConEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            for (int i = DateTime.Now.Year; i >= 1990; --i)
            {
                ListItem iteml = new ListItem(i.ToString(), i.ToString());
                DListNamSX.Items.Add(iteml);
            }
            DongHoConEntities = bll.GetDongHoConEntitiesByID();
            txtObjectID.Value = DongHoConEntities.OBJECTID.ToString();

            txtIDMaDongHo.Text = DongHoConEntities.IDMaDongHo;
            this.txtDMA.Text = DongHoConEntities.IDDMA;
            foreach (var itemtc in DMTieuChuanDongHoDAL.GetList())
            {
                if (DongHoConEntities.TieuChuanText.ToString().Trim() == itemtc.MoTa.ToString().Trim())
                    DongHoConEntities.TieuChuan = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListTieuChuan.Items.Add(iteml);
            }

            DListTieuChuan.SelectedValue = DongHoConEntities.TieuChuan.ToString();

            DListNamSX.SelectedValue = DongHoConEntities.NamSanXuat.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (DongHoConEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    DongHoConEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = DongHoConEntities.NhaSanXuat.ToString();
            
            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (DongHoConEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    DongHoConEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());//
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = DongHoConEntities.NuocSanXuat.ToString();

            this.nSTDongHo.Text = DongHoConEntities.SoThanDongHo.ToString();

            foreach (var itemvl in DMLoaiCauTaoDongHoDAL.GetList())
            {
                if (DongHoConEntities.LoaiCTDHText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    DongHoConEntities.LoaiCTDH = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListLoaiCTDH.Items.Add(iteml);
            }
            DListLoaiCTDH.SelectedValue = DongHoConEntities.LoaiCTDH.ToString();

            foreach (var itemvl in DMLCCVHDongHoDAL.GetList())
            {
                if (DongHoConEntities.LoaiCCVHText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    DongHoConEntities.LoaiCCVH = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListLoaiCCVH.Items.Add(iteml);
            }
            DListLoaiCCVH.SelectedValue = DongHoConEntities.LoaiCCVH.ToString();

            foreach (var itemc in DMLoaiMatSoDongHoDAL.GetList())
            {
                if (DongHoConEntities.LoaiMatSoText.Trim() == itemc.MoTa.ToString().Trim())
                    DongHoConEntities.LoaiMatSo = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListLoaiMatSo.Items.Add(iteml);
            }
            DListLoaiMatSo.SelectedValue = DongHoConEntities.LoaiMatSo.ToString();

            txtNgayLapDat.Text = DongHoConEntities.NgayLapDat.ToString("dd/MM/yyyy");

            this.txtMoTaViTri.Text = DongHoConEntities.MoTaViTri.ToString();

            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (DongHoConEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    DongHoConEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = DongHoConEntities.TinhTrang.ToString();

            this.txtDonViDo.Text = DongHoConEntities.DonViDo.ToString();

            this.dLuuLuongNN.Text = DongHoConEntities.LuuLuongNN.ToString();

            this.dLuuLuongLN.Text = DongHoConEntities.LuuLuongLN.ToString();

            this.dLuuLuongNN.Text = DongHoConEntities.LuuLuongNN.ToString();

            this.dLuuLuongLN.Text = DongHoConEntities.LuuLuongLN.ToString();

            this.dApLuc.Text = DongHoConEntities.ApLuc.ToString();
            this.nSoLanThay.Text = DongHoConEntities.SoLanThay.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (DongHoConEntities.DonViTCText != null)
                    if (DongHoConEntities.DonViTCText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                        DongHoConEntities.DonViTC = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViTC.Items.Add(iteml);
            }
            DListDonViTC.SelectedValue = DongHoConEntities.DonViTC.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (DongHoConEntities.DonViTKText != null)
                    if (DongHoConEntities.DonViTKText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                        DongHoConEntities.DonViTK = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViTK.Items.Add(iteml);
            }
            DListDonViTK.SelectedValue = DongHoConEntities.DonViTK.ToString();

            this.dDoChinhXac.Text = DongHoConEntities.DoChinhXac.ToString();//
            this.dCapChinhXac.Text = DongHoConEntities.CapChinhXac.ToString();
            this.txtKieu.Text = DongHoConEntities.Kieu.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (DongHoConEntities.DonViQLText != null)
                    if (DongHoConEntities.DonViQLText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                        DongHoConEntities.DonViQL = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViQL.Items.Add(iteml);
            }
            DListDonViQL.SelectedValue = DongHoConEntities.DonViQL.ToString();

            this.txtSoNha.Text = DongHoConEntities.SoNha.ToString();
            this.txtMaDuong.Text = DongHoConEntities.MaDuong.ToString();
            this.txtMaPhuong.Text = DongHoConEntities.MaPhuong.ToString();
            this.txtMaQuan.Text = DongHoConEntities.MaQuan;

            foreach (var itemkcv in DMKichCoDongHoDAL.GetList())
            {
                if (DongHoConEntities.CoDongHoText != null)
                    if (DongHoConEntities.CoDongHo.ToString().Trim() == itemkcv.MoTa.ToString().Trim())
                        DongHoConEntities.CoDongHo = itemkcv.Ma;
                ListItem iteml = new ListItem(itemkcv.MoTa.ToString(), itemkcv.Ma.ToString());
                DListKichCo.Items.Add(iteml);
            }
            if (DongHoConEntities.CoDongHo != null)
            DListKichCo.SelectedValue = DongHoConEntities.CoDongHo.ToString();

            this.txtSoHopDong.Text = DongHoConEntities.SoHopDong.ToString();

            this.txtDBHopDongNuoc.Text = DongHoConEntities.DBDongHoNuoc.ToString();

            this.txtTenKhachHang.Text = DongHoConEntities.TenThueBao.ToString();

            this.nMucDichSuDung.Text = DongHoConEntities.MucDichSuDung.ToString();

            this.txtDinhMucSuDung.Text = DongHoConEntities.DinhMucSuDung.ToString();

            this.nSoNguoiSuDung.Text = DongHoConEntities.SoNguoiSuDung.ToString();

            this.txtNhaKiemDinh.Text = DongHoConEntities.NhaKiemDinh;
            this.txtMaSoKepChi.Text = DongHoConEntities.MaSoChi;

            //foreach (var itemtsh in DMThuocSoHuuDAL.GetList()) MaSoChi
            //{
            //    if (DongHoConEntities.ThuocSoHuuText.ToString().Trim() == itemtsh.MoTa.ToString().Trim())
            //        DongHoConEntities.ThuocSoHuu = itemtsh.Ma;
            //    ListItem iteml = new ListItem(itemtsh.MoTa.ToString(), itemtsh.Ma.ToString());
            //    DListThuocSoHuu.Items.Add(iteml);
            //}
            //DListThuocSoHuu.SelectedValue = DongHoConEntities.ThuocSoHuu.ToString();

            this.txtNguoiCNC.Text = DongHoConEntities.NguoiCNCuoi.ToString();
            this.txtNgayCNC.Text = DongHoConEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtGhiChu.Text = DongHoConEntities.GhiChu;
        }//ThuocSoHuu
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        DongHoConEntities bk = new DongHoConEntities();
        DongHoConDAL DongHoConDAL = new DongHoConDAL();
        //DongHoConEntities DongHoConEntities = new DongHoConEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        DongHoConEntities DongHoConEntities = new DongHoConEntities(Convert.ToInt32(txtObjectID.Value), txtIDMaDongHo.Text,
            this.txtDMA.Text, Convert.ToInt32(DListTieuChuan.SelectedValue = DListTieuChuan.SelectedValue != "" ? DListTieuChuan.SelectedValue : "0"), 
            Convert.ToInt32(DListNamSX.SelectedValue),Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"),
            Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"), Convert.ToInt32(this.nSTDongHo.Text),
            Convert.ToInt32(DListLoaiCTDH.SelectedValue = DListLoaiCTDH.SelectedValue != "" ? DListLoaiCTDH.SelectedValue : "0"),
            Convert.ToInt32(DListLoaiCCVH.SelectedValue = DListLoaiCCVH.SelectedValue != "" ? DListLoaiCCVH.SelectedValue : "0"),
            Convert.ToInt32(DListLoaiMatSo.SelectedValue = DListLoaiMatSo.SelectedValue != "" ? DListLoaiMatSo.SelectedValue : "0"),
            DateTime.Parse(Request.Form.Get("txtNgayLapDat")),this.txtMoTaViTri.Text,
            Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
            this.txtDonViDo.Text, Convert.ToDouble(this.dLuuLuongNN.Text),Convert.ToDouble(this.dLuuLuongLN.Text),Convert.ToDouble(this.dApLuc.Text),
            Convert.ToInt32(this.nSoLanThay.Text),Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue != "" ? DListDonViTC.SelectedValue : "0"),
            Convert.ToInt32(DListDonViTK.SelectedValue = DListDonViTK.SelectedValue != "" ? DListDonViTK.SelectedValue : "0"),
            Convert.ToDouble(this.dDoChinhXac.Text),Convert.ToDouble(this.dCapChinhXac.Text),this.txtKieu.Text,
            Convert.ToInt32(DListDonViQL.SelectedValue = DListDonViQL.SelectedValue != "" ? DListDonViQL.SelectedValue : "0"),
            this.txtSoNha.Text, this.txtMaDuong.Text, this.txtMaPhuong.Text,this.txtMaQuan.Text,
            DListKichCo.SelectedValue = DListKichCo.SelectedValue != "" ? DListKichCo.SelectedValue : "",
            this.txtSoHopDong.Text, this.txtDBHopDongNuoc.Text, this.txtTenKhachHang.Text, Convert.ToInt32(this.nMucDichSuDung.Text),
            this.txtDinhMucSuDung.Text, Convert.ToInt32(this.nSoNguoiSuDung.Text), this.txtNhaKiemDinh.Text,this.txtMaSoKepChi.Text,
            this.txtNguoiCNC.Text, DateTime.Parse(Request.Form.Get("txtNgayCNC")), this.txtGhiChu.Text);
        DongHoConDAL.Update1(DongHoConEntities);
        //DongHoConDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
      //  Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}